package com.sql;

import java.sql.*;
import java.net.*;

//import java.lang.*;
//import java.util.*;

public class db {
	// 声明属性
	// 代码修改
	private Connection conn = null; // 数据库的连接初始化

	private Statement stmt = null; // sql分析器的初始化

	private ResultSet rs = null; // 数据库结果集的初始化
	/*
	 * private DBPool pool ; private Connection conn ; private ResultSet rs ;
	 * private Statement stmt ;
	 */

	// /修改完成
	//

	// --------------------------------------
	// 构造函数---------------------------------
	public db() {
		/*
		 * pool = null; conn = null ; rs = null ; stmt = null ;
		 * 
		 * try{ open(); } catch(java.sql.SQLException ex){ ex.toString(); }
		 */
	}

	// 连接数据库的方法
	public void open() throws SQLException {
		// 代码修改
		try {
			conn = DBFactory.getConnection();
		} catch (Exception e) {
		}
		/*
		 * 
		 * if( conn!=null && !conn.isClosed() ) throw new SQLException( "The
		 * connection has been established already." ) ; clear () ; try {
		 * Context env = (Context) new InitialContext().lookup("java:comp/env");
		 * DataSource pool = (DataSource) env.lookup("jdbc/agehouse"); conn =
		 * pool.getConnection(); }catch(Exception e){
		 * 
		 * System.out.println("in DBConnect:"+e); }
		 */
		// /修改完成
	}

	/*
	 * 李志峰改 查询方法 完成100%
	 */

	public ResultSet executeQuery(String sqlStmt) {
		// rs =null;

		try {
			this.clear();
			if (conn == null) {
				// conn =pool.getPool("test").getConnection();
				// conn.setAutoCommit(true);
				// conn.getAutoCommit();
				// conn.setAutoCommit(true);
				// conn =pool.getConnection();
				open();
				// conn.getca
			}
			if (conn != null) {
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sqlStmt);
				/*
				 * 不能关
				 */
				// this.clear();
				// rs.close();
				// rs.close();
				// stmt.close();
			}
		} catch (Exception e) {

		}
		return rs;

	}

	/*
	 * 李志峰改 更新方法 完成100%
	 */
	public boolean executeUpdate(String sqlStmt) {
		boolean bupdate = false;

		try {
			this.clear();
			// if(conn ==null || conn.isClosed())
			if (conn == null) {
				// conn =pool.getPool("test").getConnection();
				// conn.setAutoCommit(true);
				// conn.getAutoCommit();
				// conn.setAutoCommit(true);
				// conn =pool.getConnection();
				open();
				// conn.getca
			}
			if (conn != null) {

				conn.setAutoCommit(true);
				stmt = conn.createStatement();
				int rowCount = stmt.executeUpdate(sqlStmt);
				if (rowCount != 0)
					bupdate = true;
			}
			/*
			 * 不能关
			 */
			// this.clear();
		} catch (Exception e) {

		}

		return bupdate;
	}

	// 执行SQL语句的方法，将JDBC中的executeQuary()和executeUpdate()两个方法//合而为一，注意返回值为整形，
	public int execSQL(String sqlStmt) throws SQLException {
		try {
			this.clear();
			if (conn == null) {
				// conn.setAutoCommit(true);
				// conn.getAutoCommit();
				open();
			}
		} catch (Exception e) {
			System.out.print("The connection has been established already.");
		}
		if (sqlStmt == null)
			throw new SQLException("SQL-statement is null.");
		clear();

		stmt = conn.createStatement();
		if (sqlStmt.toUpperCase().startsWith("SELECT")) {
			rs = stmt.executeQuery(sqlStmt);
			return -1;
		} else {
			int numRow = stmt.executeUpdate(sqlStmt);
			clear();
			return numRow;
		}
	}

	// 获取字段值，参数为整形--字段次序
	public String getString(int fieldNo) throws SQLException {
		return rs.getString(fieldNo);
	}

	// 获取字段值，参数为字符串--字段名
	public String getString(String fieldName) throws SQLException {
		return rs.getString(fieldName);
	}

	// 上移指针
	public boolean previous() throws SQLException {
		if (rs == null)
			throw new SQLException("ResultSet is null.");
		return rs.previous();
	}

	// 下移指针
	public boolean next() throws SQLException {
		if (rs == null)
			throw new SQLException("ResultSet is null.");
		return rs.next();
	}

	// 指针最上
	public boolean first() throws SQLException {
		if (rs == null)
			throw new SQLException("ResultSet is null.");
		return rs.first();
	}

	// 指针最下
	public boolean last() throws SQLException {
		if (rs == null)
			throw new SQLException("ResultSet is null.");
		return rs.last();
	}

	// 清除变量，当你仅需要清除变量而不关库时可调用此方法
	public void clear() throws SQLException {
		if (rs != null)
			rs.close();
		rs = null;
		if (stmt != null)
			stmt.close();
		stmt = null;
	}

	// 清除变量并关库
	public void close() throws SQLException {
		clear();
		if (conn == null)
			throw new SQLException("This connection has been closed already.");
		if (conn.isClosed())
			throw new SQLException("This connection has been closed.");
		conn.close();
		conn = null;
		// this.open();
	}

	/*--------------转换成中文---------------*/
	@SuppressWarnings("deprecation")
	public static String toChinese(String strvalue) {
		try {
			if (strvalue == null) {
				return null;
			} else {
				// strvalue =new String(strvalue.getBytes("ISO8859_1"),"GBK");
				strvalue = URLEncoder.encode(strvalue);
				return strvalue;
			}
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 将中文字串从ISO-8859-1转换成gb2312编码。
	 * 
	 * @param text
	 * @return
	 */
	public static String iso2gb(String text) {
		String destStr = "";
		try {
			destStr = (new String(text.getBytes("gb2312"), "gb2312"));
		} catch (Exception ex) {
			return text;
			// ex.printStackTrace();
		}
		return destStr;
	}

	/*------------解释成中文--------------*/
	/**
	 * 将中文字串从GBK转换成ISO-8859-1编码。
	 * 
	 * @param text
	 * @return
	 */
	public static String GBK2iso(String text) {
		String destStr = "";
		try {
			destStr = (new String(text.getBytes("GBK"), "gb2312"));
		} catch (Exception ex) {
			return text;
			// ex.printStackTrace();
		}
		return destStr;
	}

	/*------------解释成中文--------------*/
	@SuppressWarnings("deprecation")
	public static String ExChinese(String strvalue) {
		try {
			if (strvalue == null) {
				return null;
			} else {
				strvalue = URLDecoder.decode(strvalue);
				return strvalue;
			}
		} catch (Exception e) {
			return strvalue;
		}
	}

	/*-------------数据库数据解释成中文--------------*/
	public static String DatatoChinese(String strvalue) {
		String text = "";
		try {
			text = new String(strvalue.getBytes("gb2312"), "ISO8859_1");
			return text;
		} catch (Exception e) {
			return null;
		}

	}

	public static String ISO8859_1(String strvalue) {
		String text = "";
		try {
			text = new String(strvalue.getBytes("gb2312"), "gb2312");
			return text;
		} catch (Exception e) {
			return null;
		}

	}
}
